package com.yanqu.road.server.manger;

import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.servercenter.group.CricketServerGroup;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CricketServerGroupMgr extends TempMgr {

    private static Map<Integer, Map<Integer, CricketServerGroup>> channelServerGroupMap;

    private static Map<Integer, CricketServerGroup> serverGroupMap; // groupId-group

    private static ManagerReloadListener managerReloadListener;

    @Override
    public boolean init() throws Exception {
        return reload();
    }


    @Override
    public boolean reload() throws Exception {
        reloadServerGroup();
        return true;
    }

    public static void reloadServerGroup() {
        Map<Integer, Map<Integer, CricketServerGroup>> tempChannelServerGroupMap = new ConcurrentHashMap<>();
        Map<Integer, CricketServerGroup> tempServerGroupMap = new ConcurrentHashMap<>();
        ServerBussiness.getCricketServerGroupMap(tempChannelServerGroupMap, tempServerGroupMap);
        channelServerGroupMap = tempChannelServerGroupMap;
        serverGroupMap = tempServerGroupMap;
        notifyReload();
        getLogger().info("reload cricket server group success");
    }

    public static Map<Integer, Map<Integer, CricketServerGroup>> getChannelServerGroupMap() {
        return channelServerGroupMap;
    }

    public static Map<Integer, CricketServerGroup> getServerGroupMap() {
        return serverGroupMap;
    }

    public static Map<Integer, CricketServerGroup> getServerGroupMap(int channelId) {
        return channelServerGroupMap.get(channelId);
    }

    @Deprecated
    public static CricketServerGroup getServerGroup(int channelId, int groupId) {
        if (!channelServerGroupMap.containsKey(channelId)) {
            return null;
        }
        return channelServerGroupMap.get(channelId).get(groupId);
    }

    private static void notifyReload() {
        if (managerReloadListener != null) {
            managerReloadListener.notifyReload();
        }
    }

    public static void addReloadListener(ManagerReloadListener reloadListener) {
        managerReloadListener = reloadListener;
    }


    /**
     * 获取区服分组
     */
    public static List<Long> getOpenGroupIdList(int channelId) {
        List<Long> openGroupIdList = new ArrayList<>();
        List<ServerInfo> serverInfoList = ServerBussiness.getServerInfoList();
        for (ServerInfo serverInfo : serverInfoList) {
            if (serverInfo.getChannelId() == channelId) {
                openGroupIdList.add(serverInfo.getServerId());
            }
        }
        return openGroupIdList;
    }

    public static CricketServerGroup getServerGroupByServerId(long serverId) {
        CricketServerGroup serverGroup = null;
        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
        if (null == serverInfo) {
            for (CricketServerGroup tempGroup : serverGroupMap.values()) {
                if (tempGroup.getServerList().contains(serverId)) {
                    serverGroup = tempGroup;
                    break;
                }
            }
        } else {
            Map<Integer, CricketServerGroup> dataMap = getServerGroupMap(serverInfo.getChannelId());
            if (null != dataMap) {
                for (CricketServerGroup tempGroup : dataMap.values()) {
                    if (tempGroup.getServerList().contains(serverId)) {
                        serverGroup = tempGroup;
                        break;
                    }
                }
            }
        }
        return serverGroup;
    }


    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }
}
